﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Marbles.Core.Filters
{
    public abstract class HttpResponseFilter : System.IO.Stream
    {

        public virtual string ContentEncoding
        {
            get { return string.Empty; }
        }

        public bool HeadersWritten
        {
            get { return _headerswritten; }
            set { _headerswritten = value; }
        }

        protected Stream BaseStream
        {
            get { return _basestream; }
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanSeek
        {
            get { return false; }
        }

        public override bool CanWrite
        {
            get { return _basestream.CanWrite; }
        }


        private Stream _basestream = null;
        private bool _headerswritten = false;

        protected HttpResponseFilter(Stream BaseStream)
        {
            _headerswritten = false;
            _basestream = BaseStream;
        }

        protected virtual void WriteHeaders()
        {
            HttpContext.Current.Response.AppendHeader("Content-Encoding", ContentEncoding);
            _headerswritten = true;
        }

        public override void Close()
        {
            _basestream.Close();
        }

        public override void Flush()
        {
            _basestream.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public override long Position
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        public override long Length
        {
            get { throw new NotSupportedException(); }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();            
        }

        public abstract override void Write(byte[] buffer, int offset, int count);
    }
}